package com.demo.classictag;

import java.util.Collection;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
//classic tag handler
public class MyClassicTagHandler  extends TagSupport{

	
	int count=0;
	private Collection<Integer> items;
	private  Integer[]  itemArray;
	private String var;
    private Integer step;
    
	public Integer getStep() {
		return step;
	}
	public void setStep(Integer step) {
		this.step = step;
	}



	public int getCount() {
		return count;
	}



	public Integer[] getItemArray() {
		return itemArray;
	}



	public void setItemArray(Integer[] itemArray) {
		this.itemArray = itemArray;
	}



	public String getVar() {
		return var;
	}
	
	public void setCount(int count) {
		this.count = count;
	}

	public Collection<Integer> getItems() {
		return items;
	}


	public void setItems(Collection<Integer> items) {
		this.items = items;
	}


	public void setVar(String var) {
		this.var = var;
	}


	private static final long serialVersionUID = -950990858855729636L;



	@Override
	public int doStartTag() throws JspException {

		if( getItems()!=null && getItems().size()>0){

			 setCount(0);
			setItemArray(getItems().toArray(new Integer[0]));
			// SKIP_BODY
			return  EVAL_BODY_INCLUDE;
		}
		else {
			return SKIP_BODY;
		}
	}

	@Override
	public int doAfterBody() throws JspException {

		if(getItemArray().length >count){
			pageContext.setAttribute(getVar(), getItemArray()[count]);
			 if(getStep()!=null){
				count = count + getStep();
			 }else {
			count++;
			 }
			return EVAL_BODY_AGAIN;
		}else {
			return SKIP_BODY;
		}

	}

	@Override
	public int doEndTag() throws JspException {

		System.out.println("in end tag.");
		//in do end tag.
		return super.doEndTag();
	}



}
